﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels
{
    public class FormLayoutViewModel : IHtmlString
    {
        private readonly HtmlHelper m_HtmlHelper;
        private readonly List<IField> m_Fields;

        public int Columns { get; private set; }
        public int CellSpacing { get; private set; }
        public IEnumerable<IField> Fields { get { return m_Fields; } }

        public FormLayoutViewModel(HtmlHelper htmlHelper, int columns = 1, int cellSpacing = 0)
        {
            Columns = columns;
            CellSpacing = cellSpacing;
            m_HtmlHelper = htmlHelper;
            m_Fields = new List<IField>();
        }

        public FormLayoutViewModel AddField(IField field)
        {
            m_Fields.Add(field);
            return this;
        }

        public FormLayoutViewModel AddFieldRange(IEnumerable<IField> fields)
        {
            m_Fields.AddRange(fields);
            return this;
        }

        public IHtmlString Render(int columns, int cellSpacing = 0)
        {
            Columns = columns;
            CellSpacing = cellSpacing;
            return m_HtmlHelper.Partial("FormLayout", this);
        }

        public string ToHtmlString()
        {
            return Render(Columns, CellSpacing).ToHtmlString();
        }
    }
}